<cfsilent>
	<cfset discussionCategories = $.run.it('discussionService.getDiscussionCategories',{widget=1}) />
	<cfset user = $.run.it('craftivityService.getUser',{userID=request.session.user.id}) />
	<cfset search = $.request.getValue('search') />
</cfsilent>
<cfoutput>
	<cfsavecontent variable="page">
		<style>
			.category-select{
				width:261px;
				margin-bottom:5px !important;
			}
			.discussion{
				text-align:left;
				padding:5px;
				border:1px solid ##dddddd;
				margin:4px 2px;
				position:relative;
			}
			.follower{
				background-color:rgb(224, 193, 65);
				display:inline-block;
				padding:1px 3px;
				font-size:10px;
				color:white;
				border:1px solid rgb(175, 175, 175);
				margin-right:3px;
				border-radius:3px;
				margin-top:3px;
			}
			.messages{
				max-height:250px;
				overflow-y:auto;
				overflow-x:hidden;
			}
			.messages .feedback-open-message-who{
				width:130px;
			}
		</style>
		
		<script>
			ForumAdmin = {
				toggleDiscussion:function(that,discussionID){
					var $this = $(that);
					var $discussion = $this.closest('.discussion');
					var $messages = $discussion.find('.messages');
					if($messages.hasClass('hidden')){
						$messages.removeClass('hidden');
						$this.text('hide'+$this.text().substring(4,$this.text().length));
						if(!$messages.hasClass('loaded')){
							Ajax.run({
								method:"discussionView.renderMessages",
								onsuccess:function(data){
									$messages.html(data);
									$messages.find("button[name=unfollow-discussion]").remove();
									$messages.find('button[name=respond-message]').remove();
									Main.activateTextareaResize();
								},
								args:{discussionID:discussionID}
							});
						}
					} else {
						$messages.addClass('hidden');
						$this.text('show'+$this.text().substring(4,$this.text().length));
					}
				},
				removeDiscussion:function(id){
					Main.func('discussionController','deleteDiscussion','&discussionID='+id,'Remove Discussion?');
				}
			};
			$(document).ready(function(){
				$('input[name=search]').focus();
			});
		</script>
		
		<div class="header variable-width">
			<i class="fa fa-home" onclick="Main.nav('home');"></i>
			<div class="header-title">Forum Administration</div>
			#$.render.img(path="craftivity/widget_icons/adminforum.png")#</br>
			<div>
				<form action="/craftivity/forum_admin.cfm">
					<input name="search" placeholder="search" style="margin:3px;" value="#search#">
					<button class="red">submit</button>
				</form>
			</div>
		</div>
		
		<cfloop query="discussionCategories">
			<cfset discussions = $.run.it('discussionService.getDiscussions',{
				discussionCategoryID=discussionCategories.id,
				order="if(discussion_user.id is null, 0,1) desc, discussion.created desc",
				followers=true,
				userID="",
				search=search
			}) />
 			
			<div class="widget short">
				<div class="widget-title">#discussionCategories.name#</div>
				<cfloop query="discussions">
					<cfset method = "unfollowDiscussion" />
					<cfif discussions.follow eq 0>
						<cfset method = "followDiscussion" />
					</cfif>
					#$.render.form(name='select_category',class='discussion',method='discussionController.#method#')#
						<cfset localizedDate = $.date.applyLocalization(discussions.created) />
						<div style="position:absolute;top:6px;right:0px;font-size:11px;color:green;">## #discussions.id_number#</div>
						<div style="font-weight:bold;line-height:12px;">
							<span style="color:##666666;font-weight:normal;font-size:11px;position:absolute;right:0px;top:0px;">
								<cfif dateCompare(discussions.created,user.checked_forum) eq 1><span style="color:Red;font-weight:bold;">New! </span></cfif>
								#$.format.humanizeDate(localizedDate)# - #$.format.humanizeTime(localizedDate)#
							</span> 
							#discussions.name#
						</div>
						
						<div class="messages hidden"><i class="fa fa-spin fa-spinner"></i></div>
						<cfloop list="#discussions.followers#" delimiters="|," index="i">
							<div class="follower">#i#</div>
						</cfloop><cfif listLen(discussions.followers) gt 0></br></br></cfif>
						
						</br>
						<input type="hidden" name="discussionID" value="#discussions.id#" />
						<cfif discussions.follow eq 0>
							<button class="red right small" type="submit">follow</button>
						<cfelse>
							<button class="grey right small" type="submit">unfollow</button>
						</cfif>
						<button class="right small" type="button" style="right:76px;" onclick="Main.func('discussionController','archiveDiscussion','&discussionID=#discussions.id#','Archive Discussion?');">Arkyv</button>
						<button class="right small" type="button" style="right:76px;" onclick="ForumAdmin.removeDiscussion('#discussions.id#')">del</button>
						<cfif discussions.messages gt 0>
							<button class="blue left small" type="button" onclick="ForumAdmin.toggleDiscussion(this,'#discussions.id#');">show #discussions.messages#</button>
						</cfif>
						<div style="clear:both;"></div>
					</form>
				</cfloop>
				<cfif discussions.recordcount eq 0>
					<div class="discussion" style="text-align:center;">
						<span style="color:grey;">no discussions</span>
					</div>
				</cfif>
			</div>
		</cfloop>

	</cfsavecontent>
	
	#application.v.craftivityView.renderPage(page)#
</cfoutput>

<cfsilent>
	<!--- update the fact that the user has checked the forum --->
	<cfset $.database.save(
		table="user",
		id=request.session.user.id,
		checked_forum=dateTimeFormat(now(),'yyyy-mm-dd HH:nn:ss')
	) />
</cfsilent>